#ifndef MAIN_H
#define MAIN_H

#include "Defines.h"
#include "ServerSocket.h"
#include "Room.h"
#include "user.h"
#include "MsgModPC.h"
#include <map>

using namespace std;


class CMsgSocket : public CMsgModPC
{
public:
	typedef struct
	{
		string hostname;
		unsigned char current;
		unsigned char total;
	} T_RoomInfo;
	void Init(class CMain *m) { Main = m; }
	
	//map<T_Socket, CUser *> SocketMD;
	//map<CUser::T_UserUniqueID, T_Socket> UserSocket;
	virtual void MOnRecvMsg(unsigned int type, T_LParameter lp, const DataFlow &data);

	void BeginAccept();
	void Broadcast(struct sockaddr * broadaddr);

	void SetRoom(string roomid, string hostname, unsigned char current, unsigned char total)
	{
		T_RoomInfo info = {hostname, current, total};

		EnterCriticalSection (&RoomMapLock);
		if ( total == 0 )
			RoomMap.erase(roomid);
		else
			RoomMap[roomid] = info;
		LeaveCriticalSection (&RoomMapLock);

		Broadcast(NULL);
	}
	CMsgSocket() { InitializeCriticalSection(&RoomMapLock); }

private:
	//T_SocketMD SocketMD;
	SOCKET sock;

	CRITICAL_SECTION   RoomMapLock;
	map<string, T_RoomInfo> RoomMap;
	class CMain *Main;
};

class CMain : public CMsgModPC
{
public:

	typedef map<CUser::T_UserUniqueID, CUser *> T_UserMap;
	typedef map<CGameInstance::T_InstanceID, CGameInstance *> T_InstanceMap;

	T_UserMap	Users;
	T_InstanceMap	Instances;

public:
	CMain() : CMsgModPC() {}

	CServerSocket Socket;
	CMsgSocket MsgSocket;

    virtual void MOnRecvMsg(unsigned int type, T_LParameter lp, const DataFlow &data);

	void EnterMainThread();
};





#endif